<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;

use app\api\model\Admin as AdminModel;
use app\middleware\Ky;
use app\Request;

use Firebase\JWT\JWT;
use think\Db;
use think\route\dispatch\Controller;
//use app\api\model\Admin as AdminModel;

class Login
{
    public function index()
    {
        return '您好！login';
    }

    public function login(Request $request)
    {
        $username = $request->param('username');
        $password = $request->param('password');
//        $info=AdminModel::where('username', $username)->find();
        $admin = new AdminModel();
        $info=$admin->where('username', $username)->find();
//        echo md5($password);
        if (!$info) {
            return json(['code'=>0,'msg' =>'账号不存在']);
        }
        if ($info['password'] != md5($password)) {
            return json(['code'=>0,'msg' =>'密码错误']);
        }

        //jwt
        $jwt = new JWT();
        $key = 'api123456';
        $payload = [
            'iss' => 'http://121.4.96.134:8086',
            'aud' => 'http://121.4.96.134:8086',
            'iat' => time(),
            'nbf' => time(),
            'aid' => $info['id']
        ];
        $token = $jwt::encode($payload,$key,'HS256');
        return json(['code'=>0,'msg' =>'登入成功','token'=>$token]);
    }

}
